package com.punai.customer.controller;

import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.TreeBo;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.utils.StringUtils;
import com.punai.customer.domain.CusGxFee;
import com.punai.customer.domain.CusLinkSubpkConsume;
import com.punai.customer.domain.CusLinkSubpkConsumeAddress;
import com.punai.customer.domain.vo.CusUpdateConsumeVo;
import com.punai.customer.service.ICusLinkSubpkConsumeAddressService;
import com.punai.customer.service.ICusLinkSubpkConsumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标准费用项材机耗量Controller
 * 
 * @author zhx
 * @date 2023-02-02
 */
@RestController
@RequestMapping("/cus/linkSubpkConsume")
public class CusLinkSubpkConsumeController extends BaseController
{
    @Autowired
    private ICusLinkSubpkConsumeService cusLinkSubpkConsumeService;

    @Autowired
    private ICusLinkSubpkConsumeAddressService cusLinkSubpkConsumeAddressService;

    /**
     * 查询标准费用项材机耗量地区列表
     */
    @PreAuthorize("@ss.hasPermi('cus:linkSubpkConsume:listAddress')")
    @GetMapping("/listAddress")
    public TableDataInfo list(CusLinkSubpkConsumeAddress cusLinkSubpkConsumeAddress)
    {
        startPage();
        cusLinkSubpkConsumeAddress.setCompanyId(getCompanyId());
        List<CusLinkSubpkConsumeAddress> list = cusLinkSubpkConsumeAddressService.selectCusLinkSubpkConsumeAddressList(cusLinkSubpkConsumeAddress);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('cus:linkSubpkConsume:listTree')")
    @GetMapping("/listTree/{specId}")
    public AjaxResult listtree(@PathVariable("specId") String specId)
    {
        List<TreeBo> list = cusLinkSubpkConsumeService.listTree(getCompanyId(),specId);
        return AjaxResult.success(list);
    }

    /**
     * 查询工序费用项列表
     */
    @PreAuthorize("@ss.hasPermi('cus:linkSubpkConsume:listSubpkitem')")
    @GetMapping("/listSubpkitem")
    public TableDataInfo list(CusGxFee cusGxFee)
    {
        cusGxFee.setCompanyId(getCompanyId());
        return cusLinkSubpkConsumeService.selectCusGxFeeList(cusGxFee);
    }

    /**
     * 获取工序费用项详细信息
     */
    @PreAuthorize("@ss.hasPermi('cus:linkSubpkConsume:updateSubpkitem')")
    @GetMapping(value = "/getSubpkitem/{id}/{address}")
    public AjaxResult getInfo(@PathVariable("id") String id,@PathVariable String address)
    {
        return AjaxResult.success(cusLinkSubpkConsumeService.selectCusGxFeeById(id,getCompanyId(),address));
    }

    @GetMapping("/listGxConsume/{gxId}/{address}")
    public TableDataInfo listGxConsume(@PathVariable("gxId") String gxId,@PathVariable("address") String address)
    {
        // 写的方法不过好像用不上
        return getDataTable(cusLinkSubpkConsumeService.listGxConsume(gxId,getCompanyId(),address));
    }

    @GetMapping("/listConsume/{linkGxFeatureId}/{address}")
    public TableDataInfo listConsume(@PathVariable("linkGxFeatureId") String linkGxFeatureId,@PathVariable("address") String address)
    {
        return getDataTable(cusLinkSubpkConsumeService.listConsume(linkGxFeatureId,address,getCompanyId()));
    }

    /**
     * 修改工序费用项
     */
    @PreAuthorize("@ss.hasPermi('cus:linkSubpkConsume:updateSubpkitem')")
    @PutMapping(value = "/updateSubpkitem")
    public AjaxResult updateSubpkitem(@RequestBody List<CusLinkSubpkConsume> cusLinkSubpkConsumeList)
    {
        return AjaxResult.error("该接口已废弃");
//        return toAjax(cusLinkSubpkConsumeService.updateCusGxFee(getCompanyId(),cusLinkSubpkConsumeList));
    }

    @PutMapping(value = "/updateConsume")
    public AjaxResult updateConsume(@RequestBody CusUpdateConsumeVo vo)
    {
        if(StringUtils.isEmpty(vo.getId())){
            return AjaxResult.error("获取id失败");
        }
        if(StringUtils.isEmpty(vo.getConsume().toString())){
            return AjaxResult.error("获取耗量值失败");
        }
        vo.setCompanyId(getCompanyId());
        return toAjax(cusLinkSubpkConsumeService.updateConsume(vo));
    }

    @DeleteMapping(value = "/delConsume/{id}")
    public AjaxResult delConsume(@PathVariable("id") String id)
    {
        if(StringUtils.isEmpty(id)){
            return AjaxResult.error("获取id失败");
        }
        return toAjax(cusLinkSubpkConsumeService.delConsume(id,getCompanyId()));
    }

    @PutMapping("/recoverConsume")
    public TableDataInfo recoverConsume(@RequestBody CusUpdateConsumeVo vo)
    {
        if(StringUtils.isEmpty(vo.getId())){
            throw new RuntimeException("获取id失败");
        }
        if(StringUtils.isEmpty(vo.getGxId())){
            throw new RuntimeException("获取标准费用项id失败");
        }
        return getDataTable(cusLinkSubpkConsumeService.recoverConsume(vo.getId(),vo.getGxId(), getCompanyId()));
    }
}
